package com.cpi.cornpp.purchase.program.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchase.program.form.PurchaseOrderForm;
import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderChargeView;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderView;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cs.common.utils.StringUtils;

public class SaveEditPurchaseOrderAction extends Action {
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_ADD_LABEL = "common.edit.success";//$NON-NLS-1$

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws PurchaseProgramException{
		String messageError = null;
		PurchaseOrderForm purchaseOrderForm = null;
		PurchaseOrderView purchaseOrderView = null;
		PurchaseOrderChargeView purchaseOrderChargeView = null;
		PurchaseProgramService purchaseProgramService = null;
		List purchaseOrderChargesView = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
				
		session = request.getSession(false);
		messageResources = getResources(request);
		forward = mapping.findForward(FAILURE_FORWARD);
		
		try{
			
			if (form != null){
				purchaseOrderView = new PurchaseOrderView();
				purchaseProgramService = new PurchaseProgramService();
				purchaseOrderForm = (PurchaseOrderForm) form;
				
				purchaseOrderView.setPurchaseOrderId(purchaseOrderForm.getPurchaseOrderId());
				purchaseOrderView.setPurchaseOrderWeekPeriodId(purchaseOrderForm.getBiWeekPeriodId());
				purchaseOrderView.setPurchaseOrderTransportId(purchaseOrderForm.getTransportId());
				purchaseOrderView.setPurchaseOrderPlantId((purchaseOrderForm.getPlantId().equalsIgnoreCase("-1")) ? null : purchaseOrderForm.getPlantId() );
				purchaseOrderView.setPurchaseOrderCode(purchaseOrderForm.getCode());
				purchaseOrderView.setPostFix(purchaseOrderForm.getCodeNumber());
				purchaseOrderView.setPurchaseOrderCapacity(Double.parseDouble(purchaseOrderForm.getCapacity()));
				purchaseOrderView.setCornTypeId(purchaseOrderForm.getCornTypeId());
				if ((purchaseOrderForm.getPurchaseOrderChargeIds() != null) && (purchaseOrderForm.getPurchaseOrderCosts() != null)){
					purchaseOrderChargesView = new ArrayList(purchaseOrderForm.getPurchaseOrderChargeIds().length);
					
					for (int x = 0; x < purchaseOrderForm.getPurchaseOrderChargeIds().length; x++){
						purchaseOrderChargeView = new PurchaseOrderChargeView();
						purchaseOrderChargeView.setPurchaseOrderId(purchaseOrderForm.getPurchaseOrderId());
						purchaseOrderChargeView.setPurchaseOrderChargeTypeId(purchaseOrderForm.getPurchaseOrderChargeIds()[x]);
						purchaseOrderChargeView.setPurchaseOrderAmount(Double.parseDouble((purchaseOrderForm.getPurchaseOrderCosts()[x]).replaceAll(StringUtils.COMMA, StringUtils.EMPTY_STRING)));
						purchaseOrderChargesView.add(purchaseOrderChargeView);
					}
					
					purchaseOrderView.setPurchaseOrderCosts(purchaseOrderChargesView);
				}
				
				purchaseProgramService.updatePurchaseOrder(purchaseOrderView);
				
				messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
				session.setAttribute(MESSAGE, messageError);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(PurchaseProgramException ppe){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), ppe.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, ppe);
				form.reset(mapping, request);
				throw ppe;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}	
		} catch(Exception e){
			PurchaseProgramException ppe = new PurchaseProgramException(e , TransportException.LAYER_ACTION, TransportException.ACTION_LISTS);;
			
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw ppe;
		}
		
		return forward;
	}
}
